<template>
    <!-- 仪表盘统计区域 -->
      <div class="dashboard-container">
        <!-- 顶部统计卡片 -->
        <div class="stats-grid">
          <div class="stat-card">
            <div class="stat-value">{{ stats.todayDevices }}</div>
            <div class="stat-label">今日新增设备</div>
          </div>
          <div class="stat-card">
            <div class="stat-value">{{ stats.todayOrders }}</div>
            <div class="stat-label">今日付款订单数</div>
          </div>
          <div class="stat-card">
            <div class="stat-value">¥{{ stats.todayAmount }}</div>
            <div class="stat-label">今日付款金额</div>
          </div>
        </div>
          <!-- 近6月成交额图表 -->
          <div class="data-panel">
            <h4>近6月成交额</h4>
            <ECharts 
              :option="transactionChartOption" 
              width="100%" 
              height="300px"
            />
          </div>
        </div>
</template>

<script setup>
import { ref, onMounted, nextTick, reactive } from 'vue';
import { useRouter } from 'vue-router';
import ECharts from '@/components/ECharts.vue';
import httpInstance from '@/utils/http';

// 仪表盘统计数据
const stats = reactive({
  todayDevices: 0,
  todayOrders: 0,
  todayAmount: 0
});

// 近6月成交额图表配置
const transactionChartOption = ref({
  xAxis: { type: 'category', data: [] },
  yAxis: [
    { type: 'value', name: '成交量' }, // 左侧y轴对应成交量
    { type: 'value', name: '成交额(万)' } // 右侧y轴对应成交额（万单位）
  ],
  series: [
    { name: '成交量', type: 'line', data: [] }, // 绑定成交量数据
    { name: '成交额', type: 'line', data: [] }  // 绑定成交额（万单位）数据
  ]
});

// 组件挂载后请求数据
onMounted(async () => {
  await nextTick();
  // 实际项目中启用数据请求
  await fetchTransactionData();
  await fetchDashboardStats();
});

// 请求仪表盘统计数据
const fetchDashboardStats = async () => {
  try {
    const response = await httpInstance.get('/eduservice/order/stats');
    console.log('获取仪表盘统计数据成功:', response);
    const data = response.data.data;
    
    stats.todayDevices = data.todayDeviceCount || 0;
    stats.todayOrders = data.todayPaidOrderCount || 0;
    stats.todayAmount = data.todayPaidAmount ? parseFloat(data.todayPaidAmount).toFixed(2) : '0.00';
  } catch (error) {
    console.error('获取仪表盘统计数据失败:', error);
  }
};

// 已成交订单月份数据统计
const fetchTransactionData = async () => {
  try {
    const response = await httpInstance.get('/eduservice/order/monthly-transactions');
    const monthlyData = response.data.data.list || [];
    const months = monthlyData.map(item => item.month);
    const counts = monthlyData.map(item => item.count);
    const amounts = monthlyData.map(item => item.amount);

    // ✅ 正确赋值：使用 .value
    transactionChartOption.value.xAxis.data = months;
    // ✅ 正确做法：只修改 data 字段
    transactionChartOption.value.series[0].data = counts;
    transactionChartOption.value.series[1].data = amounts;
    // 确保 series 数组有数据
    if (transactionChartOption.value.series.length === 0) {
      transactionChartOption.value.series = [
        { name: '成交量', type: 'line', data: [] },
        { name: '成交额', type: 'line', data: [] }
      ];
    }
  } catch (error) {
    console.error('获取成交额数据失败:', error);
  }
};

</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.app-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
/* 主内容区样式 */
.main-container {
  flex: 1;
  margin-left: 200px !important;
  min-height: 100vh;
  padding: 20px;
  background-color: #f5f7fa;
  overflow-y: auto;
}

/* 仪表盘样式 */
.dashboard-container {
  width: 100%;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

/* 统计卡片样式 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  background-color: #f9f9f9;
  border-radius: 6px;
  padding: 15px 20px;
  text-align: center;
  border: 1px solid #f0f0f0;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 5px;
  line-height: 1.2;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

/* 数据面板网格 */
.data-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

/* 数据面板通用样式 */
.data-panel {
  background-color: #f9f9f9;
  border-radius: 6px;
  padding: 15px 20px;
  border: 1px solid #f0f0f0;
  min-height: 350px;
}

.data-panel h4 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 8px;
  border-bottom: 1px solid #eee;
}

/* 待审核工作样式 */
.pending-items {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.item-row {
  padding: 8px 0;
  border-bottom: 1px dashed #eee;
}

.item-row:last-child {
  border-bottom: none;
}

.item-text {
  color: #0066cc;
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
}

.item-text:hover {
  color: #004080;
  text-decoration: underline;
}

/* 快捷入口样式 */
.quick-links {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
  padding-top: 10px;
}

.link-item {
  text-align: center;
}

.link-text {
  color: #0066cc;
  text-decoration: none;
  font-size: 13px;
  display: inline-block;
  padding: 5px 0;
}

.link-text:hover {
  color: #004080;
  text-decoration: underline;
}

/* 图表容器样式 */
.echarts-container {
  width: 100% !important;
  height: 300px !important;
}
</style>
